<?php
namespace ImiApp\Module\Gobang\Service;

use Imi\Bean\Annotation\Bean;
use Imi\Aop\Annotation\Inject;
use ImiApp\Exception\BusinessException;
use ImiApp\Exception\NotFoundException;
use ImiApp\Module\Gobang\Enum\GobangCell;
use ImiApp\Module\Gobang\Model\GobangGameModel;
use Imi\Log\Log;
use Imi\Log\LogLevel;

use Imi\Db\Db;

/**
 * @Bean("GobangService")
 */
class GobangService
{
    /**
     * @Inject("RoomService")
     *
     * @var \ImiApp\Module\Gobang\Service\RoomService
     */
    protected $roomService;

    /**
     * 获取战局
     *
     * @param int $roomId
     * @return \ImiApp\Module\Gobang\Model\GobangGameModel
     */
    public function getByRoomId(int $roomId): GobangGameModel
    {
        $record = GobangGameModel::find([
            'roomId'    =>  $roomId,
        ]);
        if(!$record)
        {
            throw new NotFoundException('战局不存在');
        }
        return $record;
    }

    /**
     * 创建试题
     * @param int $roomId
     * @param int $type
     * @return int[]
     */
    public function create_subject(int $roomId, int $type)
    {
        $Game = [
            'roomId'=>  $roomId,
            'type'=>  $type,
        ];

        // 判断是否在3大战内
        $res = [];
        $p = Db::query()->table('fa_kaoshi_product')->where('id', '=', $type)->select();
        $label = $p->get();
        if($label){
            $result = Db::query()->table('fa_ring_questions')
                ->where('product_id','=',$type)
                ->orderRaw('rand()')
                ->limit(5)
                ->select()
                ->getArray();
            foreach ( $result as $v){
                //单选题
                if ($v['type'] == '1') {
                    $res[] = [
                        'id' => $v['id'],
                        'type' => $v['type'],
                        'product_id' => $v['product_id'],
                        'question' => $v['question'],
                        'selectdata' => [
                            'A' => isset($v['selecta']) ? $v['selecta'] : '',
                            'B' => isset($v['selectb']) ? $v['selectb'] : '',
                            'C' => isset($v['selectc']) ? $v['selectc'] : '',
                            'D' => isset($v['selectd']) ? $v['selectd'] : '',
                        ],
                        'answer' => $v['answer'],
                        'sort' => $v['sort'],
                        'data' => $v['data'],
                        'datab' => $v['datab'],
                    ];
                }
                //单行抽字题
                elseif ($v['type'] == '2'){
                    $res[] = [
                        'id'=>$v['id'],
                        'type'=>$v['type'],
                        'product_id'=>$v['product_id'],
                        'question'=>$v['question'],
                        'sort'=>$v['sort'],
                        'data'=>$v['data'],
                        'datab'=>$v['datab'],
                    ];
                }
                //双行抽字题
                elseif ($v['type'] == '3') {
                    $res[] = [
                        'id' => $v['id'],
                        'type' => $v['type'],
                        'product_id' => $v['product_id'],
                        'question' => $v['question'],
                        'questionb' => $v['questionb'],
                        'sort' => $v['sort'],
                        'data' => $v['data'],
                        'datab' => $v['datab'],
                    ];
                }
                //看图猜题
                elseif ($v['type'] == '4') {
                    $res[] = [
                        'id'=>$v['id'],
                        'type'=>$v['type'],
                        'product_id'=>$v['product_id'],
                        'question'=>$v['question'],
                        'img_url'=>$v['img_url'],
                        'sort'=>$v['sort'],
                        'data'=>$v['data'],
                        'datab'=>$v['datab'],
                    ];
                }else{
                    return [];
                }
            }
        }

        $Game['timu'] = isset($res)?$res:[];
        return $Game;
    }

    public function create(int $roomId): GobangGameModel
    {
        $gobangGame = GobangGameModel::newInstance([
            'roomId'=>  $roomId,
            'size'  =>  15,
        ]);
        // 初始化棋盘
        $gobangGame->initMap();
        // 随机分配执子颜色
        if(1 === mt_rand(0, 1))
        {
            $gobangGame->player1Color = GobangCell::BLACK_PIECE;
            $gobangGame->player2Color = GobangCell::WHITE_PIECE;
        }
        else
        {
            $gobangGame->player1Color = GobangCell::WHITE_PIECE;
            $gobangGame->player2Color = GobangCell::BLACK_PIECE;
        }
        $gobangGame->currentPiece = GobangCell::BLACK_PIECE;
        $gobangGame->save();
        return $gobangGame;
    }

    /**
     * 落子，返回胜利方颜色
     *
     * @param integer $type
     * @param integer $roomId
     * @param integer $memberId
     * @param integer $x
     * @param integer $y
     * @return \ImiApp\Module\Gobang\Model\GobangGameModel
     */
    public function go(int $type,int $roomId, int $memberId, int $x, int $y): GobangGameModel
    {
        $room = $this->roomService->getInfo($roomId);
        $game = $this->getByRoomId($roomId);
        $currentPiece = $game->getCurrentPiece();
        if($currentPiece === $game->getPlayer1Color() && $memberId !== $room->getPlayerId1())
        {
            throw new BusinessException('非法操作');
        }
        if($currentPiece === $game->getPlayer2Color() && $memberId !== $room->getPlayerId2())
        {
            throw new BusinessException('非法操作');
        }
        $game->setCell($x, $y, $currentPiece);
        if(GobangCell::BLACK_PIECE === $currentPiece)
        {
            $game->setCurrentPiece(GobangCell::WHITE_PIECE);
        }
        else
        {
            $game->setCurrentPiece(GobangCell::BLACK_PIECE);
        }
        $game->save();
        return $game;
    }

}
